import { Component } from '@angular/core';
import { NavController, NavParams, ViewController, LoadingController ,ToastController} from 'ionic-angular';
import { BaseUI } from '../../common/baseui';
import { HttpServiceProvider } from '../../providers/http-service/http-service';
import {Storage}from '@ionic/storage';
import {RegisterPage}from'../register/register';

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
    selector: 'page-login',
    templateUrl: 'login.html'
})
export class LoginPage extends BaseUI {
    phone: any;
    password: any;
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public viewCtr: ViewController,
        public loadingCtrl: LoadingController,
        public toastgCtrl: ToastController,
        public service: HttpServiceProvider,
        public storage: Storage
    ) {
        super();
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad LoginPage');
    }
    dismiss() {
        //取消
        this.viewCtr.dismiss();
    }
    login() {
        //登录
        let loading = super.showLoading(this.loadingCtrl, '请稍后...');
        this.service
            .goLogin({
                mobile: this.phone,
                password: this.password
            })
            .then((data) => {
                console.log(data);
                loading.dismiss();
                 super.showToast(this.toastgCtrl,data.msg);
                 this.storage.set('userData',data.data);
                 this.dismiss();
            })
            .catch((err) => {
                console.log(err);
                loading.dismiss();
                super.showToast(this.toastgCtrl,err.msg);
            });
    }
    pushRegister(){
            this.navCtrl.push(RegisterPage)
    }
}
